import  numpy as np
import pandas as pd
import os
import random

def drop_duplicates():
    print(os.getcwd())
    data = pd.read_csv("data/dmax/all-dmax.csv")
    labels = list(data.columns.values)
    data.replace('\s+', '', regex=True, inplace=True)
    print(labels)
    data = data.drop_duplicates(labels[0], keep='last')
    data.to_csv("data/dmax/all-dmax2.csv", index=False)

def data_rearrange(only_am, out_path):
    # 读取train文件夹下所有csv数据，将每个元素的分类情况写入list中
    data_path = os.listdir("data/per-alloy-system")
    data_path.sort()
    data_num = len(data_path)
    total_data_num = 0

    for path in data_path:
        print(path)
        in_data = pd.read_csv("data/per-alloy-system/" + path, delimiter=r"\s+")
        labels = list(in_data.columns.values)[1:-1]
        values = in_data.values
        total_data_num += len(values)
        out_data_array = []

        #金属状态
        X, Y = values[:, 1:-1], values[:, -1:]
        size_am = 0
        if only_am:
            for j in range(len(Y)):
                str_alloy = ''
                if Y[j] == 'AM':
                    for k in range(len(labels)):
                        str_alloy = str_alloy + labels[k] + str(X[j, k])
                    out_data_array.append([str_alloy, 1])
            size_am = len(out_data_array)
        else:
            for j in range(len(Y)):
                str_alloy = ''
                for k in range(len(labels)):
                    str_alloy = str_alloy + labels[k] + str(X[j, k])
                if Y[j] == 'AM':
                    out_data_array.append([str_alloy, 1])
                    size_am=size_am+1
                else:
                    out_data_array.append([str_alloy, 0])

        if size_am > 0:
            print(out_data_array)
            out_data = pd.DataFrame(data=out_data_array, columns =["fomula", "dmax fake"])
            out_data.to_csv(out_path + '/' + path, index=False)

        # out_path = pd.read_csv("data-processed/" + path")
        print(labels)
        # for j in range(len(Y_all_comp)):
        #     if Y_all_comp[j] == 'AM':
        #         X.append
        # Y.reshape(-1, 1)

def wirte_ternary_alloy_system():
    # 读取train文件夹下所有csv数据，将每个元素的分类情况写入list中
    file_names = os.listdir("data/others/per-alloy-system/")
    file_names.sort()
    out_data = []
    for file in file_names:
        print(file)
        file_path = pd.read_csv("data/others/per-alloy-system/" + file, delimiter=r"\s+")
        labels = list(file_path.columns.values)[1:-1]
        if len(labels) == 3 and file_path.size > 20:
            out_data.append(labels)
    out_file = pd.DataFrame(data=out_data, columns=['a','b','c'])
    out_file.to_csv('data/ternary_alloy_system.csv', index=False)


def main():
    wirte_ternary_alloy_system()
#    drop_duplicates()
#    data_rearrange(only_am=True, out_path="data-processed-AM")
#    data_rearrange(only_am=False, out_path="data-processed-ALL")

if __name__ == '__main__':
    main()